<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 在线学习平台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/theme/monokai.min.css" rel="stylesheet">
    <style>
        .CodeMirror {
            height: 400px;
            border: 1px solid #ddd;
        }
        #output {
            height: 200px;
            overflow-y: auto;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            padding: 10px;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <div class="container mt-4">
        <h1 class="mb-4">Java 在线学习平台</h1>
        <div class="row">
            <div class="col-md-8">
                <div class="mb-3">
                    <label for="code-editor" class="form-label">编写你的 Java 代码：</label>
                    <textarea id="code-editor"></textarea>
                </div>
                <button id="run-btn" class="btn btn-primary">运行代码</button>
            </div>
            <div class="col-md-4">
                <div class="mb-3">
                    <label class="form-label">执行结果：</label>
                    <div id="output"></div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/clike/clike.min.js"></script>
    <script>
        // 初始化代码编辑器
        const editor = CodeMirror.fromTextArea(document.getElementById("code-editor"), {
            mode: "text/x-java",
            theme: "monokai",
            lineNumbers: true,
            autoCloseBrackets: true,
            matchBrackets: true,
            indentUnit: 4,
            tabSize: 4
        });

        // 设置默认代码
        editor.setValue(`public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}`);

        // 运行代码
        document.getElementById("run-btn").addEventListener("click", async () => {
            const output = document.getElementById("output");
            output.textContent = "正在执行...";
            
            try {
                const response = await fetch("/api/code/execute", {
                    method: "POST",
                    body: editor.getValue(),
                    headers: {
                        "Content-Type": "text/plain"
                    }
                });
                
                const result = await response.text();
                output.textContent = result;
            } catch (error) {
                output.textContent = "执行出错：" + error.message;
            }
        });
    </script>
</body>
</html> 